package com.example.day4retrofit;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.Gson;
import com.google.gson.JsonObject;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.RequestBody;

public class MainActivity extends AppCompatActivity {


    @BindView(R.id.bt1)
    Button bt1;
    @BindView(R.id.bt2)
    Button bt2;
    @BindView(R.id.bt3)
    Button bt3;
    @BindView(R.id.bt4)
    Button bt4;
    @BindView(R.id.bt5)
    Button bt5;
    @BindView(R.id.bt6)
    Button bt6;
    Gson gson;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        gson=new Gson();
    }


    @OnClick({R.id.bt1, R.id.bt2, R.id.bt3, R.id.bt4, R.id.bt5, R.id.bt6})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bt1:  //get单独传递参数
                RetrofitManager.getInstance().setBaseUrl("http://www.qubaobei.com/ios/cf/");
                //创建接口清单
                RetrofitManager.getInstance().getRetrofit()
                        .create(Api.class)
                        .getData(1,20,1)
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribeOn(Schedulers.io())
                        .subscribe(new Observer<XiaEntity>() {
                            @Override
                            public void onSubscribe(Disposable d) {

                            }

                            @Override
                            public void onNext(XiaEntity xiaEntity) {
                                String s = gson.toJson(xiaEntity);
                                Toast.makeText(MainActivity.this, ""+s, Toast.LENGTH_SHORT).show();

                            }

                            @Override
                            public void onError(Throwable e) {

                            }

                            @Override
                            public void onComplete() {

                            }
                        });
                break;
            case R.id.bt2:    //map传递参数
                RetrofitManager.getInstance().setBaseUrl("http://www.qubaobei.com/ios/cf/");
                HashMap<String, Integer> hashMap = new HashMap<>();
                hashMap.put("stage_id",1);
                hashMap.put("limit",20);
                hashMap.put("page",1);

                //创建接口清单
                RetrofitManager.getInstance().getRetrofit()
                        .create(Api.class)
                        .getData(hashMap)
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribeOn(Schedulers.io())
                        .subscribe(new Observer<XiaEntity>() {
                            @Override
                            public void onSubscribe(Disposable d) {

                            }

                            @Override
                            public void onNext(XiaEntity xiaEntity) {
                                String s = gson.toJson(xiaEntity);
                                Toast.makeText(MainActivity.this, ""+s, Toast.LENGTH_SHORT).show();

                            }

                            @Override
                            public void onError(Throwable e) {

                            }

                            @Override
                            public void onComplete() {

                            }
                        });
                break;
            case R.id.bt3:
                RetrofitManager.getInstance().setBaseUrl("https://www.wanandroid.com/");
                //创建接口清单
                RetrofitManager.getInstance().getRetrofit()
                        .create(Api.class)
                        .getlogin("123456","123456")
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribeOn(Schedulers.io())
                        .subscribe(new Observer<String>() {
                            @Override
                            public void onSubscribe(Disposable d) {

                            }

                            @Override
                            public void onNext(String s) {
                                Toast.makeText(MainActivity.this, ""+s, Toast.LENGTH_SHORT).show();
                            }

                            @Override
                            public void onError(Throwable e) {
                                Toast.makeText(MainActivity.this, ""+e, Toast.LENGTH_SHORT).show();
                                Log.i("TAG", "onError: ------------>"+e);
                            }

                            @Override
                            public void onComplete() {

                            }
                        });
                break;
            case R.id.bt4:

                break;
            case R.id.bt5:
                RetrofitManager.getInstance().setBaseUrl("https://www.wanandroid.com/user/login/");
                JsonObject jsonObject = new JsonObject();
                RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), jsonObject.toString());
                //创建接口清单
                RetrofitManager.getInstance().getRetrofit()
                        .create(Api.class)
                        .getlogin("123456","123456")
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribeOn(Schedulers.io())
                        .subscribe(new Observer<String>() {
                            @Override
                            public void onSubscribe(Disposable d) {

                            }

                            @Override
                            public void onNext(String s) {
                                Toast.makeText(MainActivity.this, ""+s, Toast.LENGTH_SHORT).show();
                            }

                            @Override
                            public void onError(Throwable e) {
                                Toast.makeText(MainActivity.this, ""+e, Toast.LENGTH_SHORT).show();
                                Log.i("TAG", "onError: ------------>"+e);
                            }

                            @Override
                            public void onComplete() {

                            }
                        });
                break;
            case R.id.bt6:
                break;
        }
    }
}
